<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<script type="text/javascript" src="<?php echo STATIC_DOMAIN;?>js/highcharts/js/highcharts.src.js"></script>
<div class="middle myclear">
	<div class="pad10">
			
			<h2 class="marbottom10 martop20">
				<?php echo $campaign_lang['all_campaign'];?>
			</h2>

			<div class="payment-listbox padbottom10 myclear" >
				
				<table class="mbd-timebox" cellpadding="0" cellspacing="0" border="0">	
					<tr>
						<td class="padright10 ver-middle">
							<input class="input-time" type="text" id="date" value="<?php echo date('d/m/Y',strtotime($fromdate));?> <?php echo $campaign_lang['to'];?> <?php echo date('d/m/Y', strtotime($todate));?>" />
						</td>
						
						<td class="padright10 ver-middle">
							<select class="payment-by-month" id="status">
                                <option value="123" <?php echo $status == '123' ? 'selected="selected"' : '';?>><?php echo $campaign_lang['all'];?></option>
								<option value="1" <?php echo $status == '1' ? 'selected="selected"' : '';?> ><?php echo $campaign_lang['running'];?></option>
                                <option value="5" <?php echo $status == '5' ? 'selected="selected"' : '';?> ><?php echo $campaign_lang['paused'];?></option>
                                <option value="-1" <?php echo $status == '-1' ? 'selected="selected"' : '';?> ><?php echo $campaign_lang['completed'];?></option>
                                <option value="3" <?php echo $status==3 ? 'selected="selected"':'' ;?>><?php echo $campaign_lang['het_ngan_sach'];?></option>
                                <option value="4" <?php echo $status==4 ? 'selected="selected"':'' ;?>><?php echo $campaign_lang['outofmoney'];?></option>
                            </select>
						</td>
						
						<td class="padright10 ver-middle">
							<select class="payment-by-month" id="filterGroup">
								<option value="-1">Tất cả</option>
								<?php
								foreach($lstGroup as $gr)
								{
								?>
									<option <?php echo $groupid == $gr['groupid'] ? 'selected="selected"' : '';?> value="<?php echo $gr['groupid']; ?>"><?php echo $gr['groupname']; ?></option>
								<?php
								}
								?>
							</select>
						</td>
						
						<td class="padright10 ver-middle">
							<input style="border:1px solid #e3e3e3;" type="text" name="txtContract" id="txtContract" class="text-999 pad5" onblur="if(this.value==''){this.value='Gõ mã hợp đồng';}" onfocus="if(this.value=='Gõ mã hợp đồng'){this.value='';}" value="<?php if(isset($contract)&&($contract!='')){echo $contract;}else{echo 'Gõ mã hợp đồng';} ?>" />
						</td>
						
						<td class="padright10 ver-middle">
							<input type="text" name="txtUserFilter" id="txtUserFilter" class="superblyTagfield" allowNewTags="false"  number="5000" readonly="readonly" preset="<?php echo $strUserId; ?>" />
						</td>
						
                        <td class="ver-middle">
                            <input type="button" class="btn" value="Xem" onclick="campFilter('<?php echo site_url('campaign'); ?>');" />
                        </td>
                        
					</tr>
				</table>
				<div id="campaigndiv">
					<table class="martop5 tbl-list" width="100%" cellpadding="0" cellspacing="0" border="0" id="table">
		                <thead>
							<tr>
								<th class="td-label bold text-center">
									<p class="ver-middle" style="height:33px;line-height: 33px;"><?php echo $campaign_lang['#'];?></p>
								</th>
								
								<th class="td-label bold text-center">
									<p class="ver-middle" style="height:33px;line-height: 33px;"><?php echo $campaign_lang['campaign'];?></p>
								</th>
								
								<th class="td-label bold text-center">
									<p class="ver-middle" style="height:33px;line-height: 33px;"><?php echo $campaign_lang['status'];?></p>
								</th>
								
								<th class="td-label bold text-center">
									<p class="ver-middle" style="height:33px;line-height: 33px;"><?php echo $campaign_lang['budget'];?></p>
								</th>
								
								<th class="td-label bold text-center">
									<p class="ver-middle" style="height:33px;line-height: 33px;">Click</p>
								</th>
								
		                        <th class="td-label bold text-center">
									<p class="ver-middle" style="height:33px;line-height: 33px;">Impressions</p>
								</th>
								
		                        <th class="td-label bold text-center">
									<p class="ver-middle" style="height:33px;line-height: 33px;">Unique Visitor</p>
								</th>
		                        
								<th class="td-label bold text-center">
									<p class="ver-middle" style="height:33px;line-height: 33px;">CTR (%)</p>
								</th>
								
								<th class="td-label bold text-center">
									<p class="ver-middle" style="height:33px;line-height: 33px;"><?php echo $campaign_lang['spent'];?></p>
								</th>
								<th class="td-label bold text-center">
									<p class="ver-middle" style="height:33px;line-height: 33px;width:80px;">Mã hợp đồng</p>
								</th>
								<th class="td-label bold text-center">
									<p class="ver-middle" style="height:33px;line-height: 33px;">
										username
									</p>
								</th>
								<th class="td-label bold text-center"></th>
							</tr>
		                </thead>
		                <tbody>
							<?php $index=1+($cPage-1)*$numShowItem;
		                    foreach($info as $if)
		                    { 
		                    ?>
								<tr>
									<td class="ver-middle">
										<p class="pad5 text-center">
											<?php echo $index;?>
										</p>
									</td>
									
									<td class="ver-middle">
										<p class="pad5">
			                                <a href="<?php echo site_url('campaigndetail/index?campaignid='.$if['campaignid'].'&fromdate='.date('Y-m-d',strtotime($fromdate)).'&todate='.date('Y-m-d',strtotime($todate)));?>">
											    <?php echo $if['campaign'];?>
			                                </a>
										</p>
									</td>
									
									<td class="ver-middle">
										<p class="pad5 text-center edit">
			                                <input type="hidden" value="<?php echo site_url('campaign/edit?campaignid='.$if['campaignid'].'&type=1');?>" class="hddEditLink"/>
											<?php //echo $if['status'];
                                                if($if['status']==1)
                                                    echo '<img class="cursor-pointer" alt="" src="/images/imgrun.gif" title="'.$campaign_lang['running'].'"/>';
                                                elseif($if['status']==-1)
                                                    echo '<img class="cursor-pointer" alt="" src="/images/imgok.gif" title="'.$campaign_lang['completed'].'"/>';
                                                elseif($if['status']==5)
                                                    echo '<img class="cursor-pointer" alt="" src="/images/imgpause.gif" title="'.$campaign_lang['paused'].'"/>';
                                                elseif($if['status']==2)
                                                    echo '<img class="cursor-pointer" alt="" src="/images/temp_deleted.gif" title="'.$campaign_lang['delete'].'"/>';
                                                elseif($if['status']==4)
                                                    echo '<img class="cursor-pointer" alt="" src="/images/imgout-of-money.png" title="'.$campaign_lang['outofmoney'].'"/>';
                                                elseif($if['status']==3)
                                                    echo '<img class="cursor-pointer" alt="" src="/images/waittorun.gif" title="'.$campaign_lang['waittorun'].'"/>';
                                                else
                                                    echo $if['status'];
			                                ?>
										</p>
									</td>
			                        
									<td class="ver-middle">
										<div class="pad5 text-center">
				                            <?php if($if['isperday']==1)
				                            {
				                                ?>
												<p>
													<?php echo number_format($if['maxValueDay']);?>
												</p>
												<p class="text-999">
				                                    <?php echo $campaign_lang['byDate'];?>
				                                </p>
				                            <?php
				                            }else{
				                            ?>
				                               <p>
													<?php echo $if['ExpireValue'];?>
												</p>
												<p class="text-999">
				                                    <?php echo $campaign_lang['byCam'];?>
				                                </p>
				                            <?php
				                            }
				                            ?>
										</div>
									</td>
									
									<td class="ver-middle">
										<p class="text-right">
											<?php echo $if['clicks'];?>
										</p>
									</td>
									
			                        <td class="ver-middle">
										<p class="text-right">
											<?php echo $if['impressions'];?>
										</p>
									</td>
			                        
			                        <td class="ver-middle">
										<p class="text-right">
											<?php echo $if['uviews'];?>
										</p>
									</td>
			                        
									<td class="ver-middle">
										<p class="text-right">
											<?php echo $if['CTR'];?>
										</p>
									</td>
									
									<td class="ver-middle">
										<p class="text-right">
											<?php echo $if['priced'];?>
										</p>
									</td>
									<td class="ver-middle">
										<p class="text-left">
											<?php echo $if['contract'];?>
										</p>
									</td>
									<td class="ver-middle">
										<p class="text-left">
											<?php echo $if['username'];?>
										</p>
									</td>
									<td class="ver-middle">
										<a href="<?php echo site_url('campaign/exportIPs/?id='.$if['campaignid'].'&fromdate='.$fromdate.'&todate='.$todate);?>">
											Export IPs
										</a>
									</td>
								</tr>
							<?php
	                        	$index++;
	                        }
		                    ?>
		                </tbody>
					</table>
					
					<table class="tbl-list brv-paging martop10 float-right" border="1" cellpadding="0" cellspacing="0">
						<tr>
							<td class="brv-paging-name">
								<?php echo $campaign_lang['page'];?> <?php echo $cPage; ?>/<?php echo $pCount; ?>
							</td>
							
							<td class="brv-paging-pre-next" width="48">
								<img class="brv-img-pre" alt="previous" src="/images/imgpre.gif" <?php echo ($cPage > 1) ? 'onclick="campPaging(\'' . site_url("campaign/?fromdate=$fdateUrl&todate=$tdateUrl&status=$status&cpage=" . ($cPage - 1) . "&numitem=$numShowItem&groupid=$groupid&user=$strUserId&contract=$contract" ) . '\');"' : ''; ?>/>
								<img class="brv-img-next" alt="next" src="/images/imgnext.gif" <?php echo ($cPage < $pCount) ? 'onclick="campPaging(\'' . site_url("campaign/?fromdate=$fdateUrl&todate=$tdateUrl&status=$status&cpage=" . ($cPage + 1) . "&numitem=$numShowItem&groupid=$groupid&user=$strUserId&contract=$contract" ) . '\');"' : ''; ?> />
							</td>
							
							<td class="brv-paging-num-item">
								<select id="slcNumItem" onchange="campPaging('<?php echo site_url("campaign/?fromdate=$fdateUrl&todate=$tdateUrl&status=$status&cpage=1&groupid=$groupid&user=$strUserId"); ?>' + '&numitem=' + this.value);">
									<option value="10" <?php echo ($numShowItem == 10) ? 'selected="selected"' : '';?> >10</option>
									<option value="20" <?php echo ($numShowItem == 20) ? 'selected="selected"' : '';?> >20</option>
									<option value="50" <?php echo ($numShowItem == 50) ? 'selected="selected"' : '';?>>50</option>
								</select>
							</td>
						</tr>
					</table>
                    <table cellpadding="0" cellspacing="0" border="0" class="padtop20">
                        <tr>
                            <td>
                                <a href="javascript:void(0)" class="graybtn marright5">
        							<span <?php echo ($type==0) ? 'class="curr"':'' ;?> onclick="ajax_chart(0);" id="clicks">Clicks</span>
        						</a>
                            </td>
                            <td>
                                <a href="javascript:void(0)" class="graybtn marright5">
        							<span <?php echo ($type==1) ? 'class="curr"':'' ;?> onclick="ajax_chart(1);" id="impressions"><?php echo $campaign_lang['impressions'];?></span>
        						</a>
                            </td>
                            <?php if(count($lstCampaign)>0){ ?>
                            <td>
                                <p class="marright5">
                                    <select multiple="multiple" id="selector" name="selector[]" multiple="multiple" size="5">
                                        <?php foreach ($lstCampaign as $lCampaign) { ?>
                                            <option value="<?php echo $lCampaign['campaignid'] ?>" <?php echo in_array($lCampaign['campaignid'],$temp)? 'selected="selected"':'' ;?>><?php echo strlen($lCampaign['campaignname']) > 60 ? substr($lCampaign['campaignname'], 0, 60) . '...' : $lCampaign['campaignname']; ?></option>      
                                        <?php } ?>
                                    </select>
                                </p>
                            </td>
                            <td>
                                <a href="javascript:void(0)" class="graybtn">
        							<span id="ren">OK</span>
        						</a>
                            </td>
                            <?php } ?>
                            <td>
                                <a class="marleft10">
        							<img id="loading" src="/images/ajaxLoading.gif" class="martop5 ver-middle" style="display: none;"/>
        						</a>
                            </td>
                        </tr>
                    </table>
                    <table cellpadding="0" cellspacing="0" border="0" width="100%" class="padtop20">
                        <tr>
                            <td>
                                <div id="my_chart"></div>
                            </td>
                        </tr>
                    </table>
    				</div> 
                </div>
			</div>
			
		</div>
		<!-- important. fix for ie 6. Don't remove -->
		<!--[if IE 6]><br class="clearboth" /><![endif]-->
	</div> <!-- end page content -->
</div>
<style>
.superblyTagfieldDiv{
	width: 210px;
}
</style>
<script type="text/javascript">
	function campPaging(url)
	{
		window.location = url;
		return;
	}
    
	function ajax_chart(val)
    {
        var temp =$('#date').val();
        var arrTime = temp.toString().split(' <?php echo $campaign_lang['to']?> ');
        var fromdate = arrTime[0];
        var todate = arrTime[1];
        var strReplace="/";
        arrFromdate=fromdate.toString().split('/');
        fromdate=arrFromdate[2]+'-'+arrFromdate[1]+'-'+arrFromdate[0];
        arrTodate=todate.toString().split('/');
        todate=arrTodate[2]+'-'+arrTodate[1]+'-'+arrTodate[0];
        
        var about = [];
        $("input[name='selector[]']:checked").each(function(){about.push($(this).val());});
        lstCampaign = about.join(",");
        var state=$('#status').val();
        var strURL="<?php echo site_url('campaign/filter_chart?fromdate=');?>"+fromdate+"&todate="+todate+"&type="+val+"&lstcampaign="+lstCampaign+'&state='+state;
        var onSuccess= function(data) 
        {
            if(data=='error')
            {
                window.location = '<?php echo site_url('banner/error')?>';
            }else if(data=='unlogin'){
                window.location = '<?php echo site_url('login')?>';
            }else{
                if(val==0)
                {
                    $('#impressions').removeClass("curr");
                    $('#clicks').addClass("curr");
                }else{
                    $('#clicks').removeClass("curr");
                    $('#impressions').addClass("curr");
                }
                $('#my_chart').html(data);
                $('#loading').hide();
            }
        };
        $('#loading').show()
        getAjax(strURL,"",'', '', '', false, onSuccess);
    }
    
    $("#ren").click( function() 
    {   
        var about = [];
        $("input[name='selector[]']:checked").each(function(){about.push($(this).val());});
        var lstCampaign = about.join(",");
        var temp =$('#date').val();
        var arrTime = temp.toString().split(' <?php echo $campaign_lang['to']?> ');
        var fromdate = arrTime[0];
        var todate = arrTime[1];
        var strReplace="/";
        arrFromdate=fromdate.toString().split('/');
        fromdate=arrFromdate[2]+'-'+arrFromdate[1]+'-'+arrFromdate[0];
        arrTodate=todate.toString().split('/');
        todate=arrTodate[2]+'-'+arrTodate[1]+'-'+arrTodate[0];
        var val = ($('#clicks').attr('class')=='curr') ? 0:1;
        var state=$('#status').val();
        var strURL="<?php echo site_url('campaign/filter_chart?fromdate=');?>"+fromdate+"&todate="+todate+"&type="+val+"&lstcampaign="+lstCampaign+'&state='+state;
        var onSuccess= function(data) 
        {
            if(data=='error')
            {
                window.location = '<?php echo site_url('banner/error')?>';
            }else if(data=='unlogin'){
                window.location = '<?php echo site_url('login')?>';
            }else{
                if(val==0)
                {
                    $('#impressions').removeClass("curr");
                    $('#clicks').addClass("curr");
                }else{
                    $('#clicks').removeClass("curr");
                    $('#impressions').addClass("curr");
                }
                $('#my_chart').html(data);
                $('#loading').hide();
            }
        };
        $('#loading').show()
        getAjax(strURL,"",'', '', '', false, onSuccess);
        return false;
    });
    
    $(document).ready( function() 
    {
        $("#selector").multiSelect(
        {
    	     selectAll: false,
             noneSelected: 'Xin chọn chiến dịch!',
             oneOrMoreSelected :'% chiến dịch được chọn.',
             warningMsg:'Bạn không được chọn quá 3 chiến dịch'
        });
        
    	<?php
	 	if(!empty($info))
	 	{
	 	?>
	    	$(function() {        
			  $(".edit").editable('',{ 
			    data   : "{'1':'<?php echo $campaign_lang['run'];?>','5':'<?php echo $campaign_lang['paused'];?>','completedCam':'<?php echo $campaign_lang['completed'];?>','selected':'<?php echo $if['status']?>'}",
			    type:"select",
			    submit : 'OK',
			    indicator : 'Wait...',
			    id : 'hk',
	            typeEdit:true,
			    name : 'ns',
			    css : 'inherit',
			    cssclass : "editable",
			    onblur    : "cancel"
			  });  
			});
		<?php
		}
		?>
		
    	// init city location tag  suggestion
		$('.superblyTagfield').css('display', 'none');		
		var allowNewTags = false;
		var showTagsNumber = 1000;
		var preset = new Array();
		var url = '/admin/campaign/get_suggest_user';
		if($('#txtUserFilter').attr('allowNewTags') == 'false'){
			allowNewTags = false;
		}
    	if($('#txtUserFilter').attr('number') != null){
			showTagsNumber = $('#txtUserFilter').attr('number');
		}
				
		if($('#txtUserFilter').attr('preset') != null){
			preset = $('#txtUserFilter').attr('preset').split(',');
		}
		
		var fiterUserSuggestCallBack = function()
		{
			
		};
		
		superblyTagField($('#txtUserFilter'), url, preset, allowNewTags, showTagsNumber, '/images/ajaxLoading.gif', false, fiterUserSuggestCallBack, fiterUserSuggestCallBack, 'Gõ username để lựa chọn', '', '200');
    	
    	
    	
        //datepicker
        $('#date').DatePicker({
            format: 'd/m/Y',
            date: ['<?php echo date('d/m/Y',strtotime($fromdate));?>','<?php echo date('d/m/Y',strtotime($todate));?>'],
            current: '<?php echo date('d/m/Y');?>',
            calendars: 2,
            mode: 'range',
            starts: 1, 
            onChange: function(formated, dates) 
            {
                var about = [];
                $("input[name='selector[]']:checked").each(function(){about.push($(this).val());});
                lstCampaign = about.join(",");
                var type = ($('#clicks').attr('class')=='curr') ? 0:1;
            
                $('#loading').show();
                $('#date').val(formated.join(' <?php echo $campaign_lang['to'];?> '));
                $('#date').DatePickerHide();
                var arrTime = formated.toString().split(',');
                var fromdate = arrTime[0];
                var todate = arrTime[1];
                arrFromdate=fromdate.toString().split('/');
                fromdate=arrFromdate[2]+'-'+arrFromdate[1]+'-'+arrFromdate[0];
                arrTodate=todate.toString().split('/');
                todate=arrTodate[2]+'-'+arrTodate[1]+'-'+arrTodate[0];
                var status =$('#status').val();
                url ='<?php echo site_url('campaign/?status='.$status.'&cpage='.$cPage.'&numitem='.$numShowItem.'&groupid='.$groupid.'&user='.$strUserId)?>&fromdate='+fromdate+'&todate='+todate;
                window.location=url;
            }
        });
        
        
        <?php
	   	foreach($lstUser as $user)
	   	{
		?>
			filterUserAddUser('#txtUserFilter', '<?php echo $user['username']; ?>', '<?php echo $user['user_id']; ?>', 'Gõ username để lựa chọn');
		<?php
	   	}
	    ?>
    });
	
	// init category
    function filterUserAddUser(tagid, name, catid, strInputDefault)
    {
    	strInputDefault = (typeof(strInputDefault) === 'undefined' || strInputDefault === '') ? '' : strInputDefault;
		$(tagid).parent('.superblyTagInputItem').before("<li class='superblyTagItem'><span>" + name + "</span><a><img alt='' src='/images/x.gif' /></a><input type='hidden' class='superblyTagItemHddId' value='" + catid + "' /></li>");
		
		var val = $(tagid).val();
		if(val == '')
		{
			$(tagid).val(catid);	
		}
		else
		{
			$(tagid).val($(tagid).val() + ',' + catid);	
		}
		
		var new_index = $(tagid).parent('.superblyTagInputItem').parent('.superblyTagItems').children('.superblyTagItem').size()-1;	
		$($(tagid).parent('.superblyTagInputItem').parent('.superblyTagItems').children('.superblyTagItem')[new_index]).children('a').click(function(e){
			//var value = $($(this).parent('.superblyTagItem').children('span')[0]).text();
			var value = $($(this).parent('.superblyTagItem').children('.superblyTagItemHddId')).val();
			removeSuggestCat(value, tagid, strInputDefault);
		});
    }
    
    function campFilter(url)
    {
    	var strDate = $('#date').val();
    	var arrDate = strDate.split(' ');
    	var arrStartDate = $.trim(arrDate[0]).split('/');
    	var strStartDate = arrStartDate[2] + '-' + arrStartDate[1] + '-' + arrStartDate[0];
    	
    	var arrEndDate = $.trim(arrDate[2]).split('/');
    	var strEndDate = arrEndDate[2] + '-' + arrEndDate[1] + '-' + arrEndDate[0];
    	arrDate = null;
    	arrStartDate = null;
    	arrEndDate = null;
    	
    	var status = $('#status').val();
    	var group = $('#filterGroup').val();
    	var user = $('#txtUserFilter').val();
    	var txtcontract = $('#txtContract').val();
    	var numitem = $('#slcNumItem').val();
    	
    	window.location = url + '/?fromdate=' + strStartDate + '&todate=' + strEndDate + '&status=' + status + '&cpage=1' + '&numitem=' + numitem + '&groupid=' + group + '&user=' + user + '&contract=' + txtcontract;
    }
    
    ajax_chart(0);
</script>